﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DBContext;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Common;
using ZoneLine.Models;

namespace ZoneLine.ViewModels
{
    public class InventoryViewModel : ObservableObject
    {
        private readonly IModelDAL _dal;
        public InventoryViewModel(IModelDAL dAL)
        {
            this._dal = dAL;
            var mTList = Enum.GetValues(typeof(eMaterialType))
                                      .Cast<eMaterialType>()
                                      .Select(o => new { Key = (int)o, Descr = o.GetDisplayDescription(), Content = o }).ToList();
            this.MaterialTypeList = new ObservableCollection<object>(mTList);
            this.QueryCmd.Execute(null);

            //this.InventoryInfo.MainBatchCode = "$P000000001010900638$V0018000040$L25031901$Q500$O4500125969$S80$E$N64";
        }


        #region Props

        private int _qty = 1;
        /// <summary>
        /// 库存量
        /// </summary>
        public int Qty
        {
            get { return _qty; }
            set { SetProperty(ref _qty, value); SurplusQty = value; }
        }

        private int _surplusQty;
        /// <summary>
        /// 余量
        /// </summary>
        public int SurplusQty
        {
            get { return _surplusQty; }
            set { SetProperty(ref _surplusQty, value); }
        }


        private InventoryInfoModel _inventoryInfo = new InventoryInfoModel();

        public InventoryInfoModel InventoryInfo
        {
            get { return _inventoryInfo; }
            set { SetProperty(ref _inventoryInfo, value); }
        }


        private ObservableCollection<object> _materialTypeList = new ObservableCollection<object>();

        public ObservableCollection<object> MaterialTypeList
        {
            get { return _materialTypeList; }
            set { SetProperty(ref _materialTypeList, value); }
        }

        private ObservableCollection<InventoryInfoModel> _dataList = new ObservableCollection<InventoryInfoModel>();

        public ObservableCollection<InventoryInfoModel> DataList
        {
            get { return _dataList; }
            set { SetProperty(ref _dataList, value); }
        }


        private string _queryMaterialNo;

        public string QueryMaterialNo
        {
            get { return _queryMaterialNo; }
            set { SetProperty(ref _queryMaterialNo, value); }
        }

        private eMaterialType? _queryMaterialType;

        public eMaterialType? QueryMaterialType
        {
            get { return _queryMaterialType; }
            set { SetProperty(ref _queryMaterialType, value); }
        }

        private string _queryBatchNo;

        public string QueryBatchNo
        {
            get { return _queryBatchNo; }
            set { SetProperty(ref _queryBatchNo, value); }
        }
        #endregion

        #region Commands

        public RelayCommand SaveCmd => new RelayCommand(() =>
        {
            InventoryInfo.StationCode = "OP50";
            InventoryInfo.Qty = Qty;
            InventoryInfo.SurplusQty = SurplusQty;
            InventoryInfo.CreateTime = DateTime.Now;

            if (string.IsNullOrEmpty(InventoryInfo.MainBatchCode))
            {
                HandyControl.Controls.MessageBox.Error("主批次码不能为空！", "温馨提示");
                return;
            }

            if (string.IsNullOrEmpty(InventoryInfo.MaterialNo))
            {
                HandyControl.Controls.MessageBox.Error("物料编号不能为空！", "温馨提示");
                return;
            }

            if (string.IsNullOrEmpty(InventoryInfo.BatchNo))
            {
                HandyControl.Controls.MessageBox.Error("批次不能为空！", "温馨提示");
                return;
            }

            if (InventoryInfo.MaterialType <= 0)
            {
                HandyControl.Controls.MessageBox.Error("物料类型不能为空！", "温馨提示");
                return;
            }

            var res = _dal.AddModel(InventoryInfo);
            if (res)
            {
                HandyControl.Controls.MessageBox.Success("录入成功！", "温馨提示");
                InventoryInfo.MainBatchCode = "";
                InventoryInfo.MaterialNo = "";
                InventoryInfo.BatchNo = "";
                Qty = 1;


                QueryCmd.Execute(null);
            }
            else {
                HandyControl.Controls.MessageBox.Error("录入失败！", "温馨提示");
            }
            //Console.WriteLine(JsonConvert.SerializeObject(InventoryInfo));
        });


        public RelayCommand QueryCmd => new RelayCommand(() =>
        {
            GetInventorys();
        });

        public RelayCommand<string> MainBatchCodeEnterCmd => new RelayCommand<string>((obj) =>
        {
            try
            {
                if (!string.IsNullOrEmpty(obj))
                {
                    var arr = obj.Split('$');
                  
                    InventoryInfo.MaterialNo = arr.FirstOrDefault(o => o.StartsWith("P")).Replace("P", "");
                    InventoryInfo.BatchNo = arr.FirstOrDefault(o => o.StartsWith("L")).Replace("L", "");
                    
                    Qty = Convert.ToInt32(arr.FirstOrDefault(o => o.StartsWith("Q")).Replace("Q", ""));
                }
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error("主批次码信息错误！", "温馨提示");
            }
        });
        #endregion


        #region Methods
        /// <summary>
        /// 获取物料信息
        /// </summary>
        private void GetInventorys()
        {

            var list = DbConfig.DbContext.Select<InventoryInfoModel>()
                .WhereIf(!string.IsNullOrEmpty(QueryMaterialNo), o => o.MaterialNo == QueryMaterialNo)
                .WhereIf(QueryMaterialType.HasValue, o => o.MaterialType == QueryMaterialType)
                .WhereIf(!string.IsNullOrEmpty(QueryBatchNo), o => o.BatchNo == QueryBatchNo).ToList();
            DataList.Clear();
            DataList = new ObservableCollection<InventoryInfoModel>(list);
        }

        #endregion


    }
}
